package chooongg.base.widget.titleBar

import android.app.Activity
import android.content.Context
import android.graphics.drawable.Drawable
import android.util.AttributeSet
import android.view.View
import androidx.annotation.DimenRes
import androidx.annotation.DrawableRes
import androidx.annotation.StringRes
import androidx.appcompat.widget.AppCompatImageView
import androidx.appcompat.widget.AppCompatTextView
import chooongg.base.R
import chooongg.base.utils.*
import kotlinx.android.synthetic.main.titlebar_parent.view.*
import kotlinx.android.synthetic.main.titlebar_center.view.*

class CenterTitleBar @JvmOverloads constructor(
    context: Context,
    attrs: AttributeSet? = null,
    defStyleAttr: Int = 0
) : AbsTitleBar(context, attrs, defStyleAttr) {

    override fun initLayout() {
        View.inflate(context, R.layout.titlebar_center, title_content)
    }

    override fun color(color: Int) = apply {
        title_tv_title.setTextColor(color)
        title_tv_subtitle.setTextColor(color)
        title_tv_navigation.setTextColor(color)
        title_iv_navigation.setColorFilter(color)
        title_tv_operation.setTextColor(color)
        title_iv_operation.setColorFilter(color)
        title_tv_neutral.setTextColor(color)
        title_iv_neutral.setColorFilter(color)
    }

    override fun title(title: CharSequence?) = apply {
        title_tv_title.isSelected = true
        title_tv_title.text = title
    }

    override fun getTitle(): String {
        return title_tv_title.text.toString()
    }

    override fun title(@StringRes resId: Int) = apply {
        title_tv_title.isSelected = true
        title_tv_title.setText(resId)
    }

    override fun subTitle(subTitle: CharSequence?) = apply {
        if (subTitle.isNullOrEmpty()) title_tv_subtitle.gone()
        else {
            title_tv_subtitle.isSelected = true
            title_tv_subtitle.text = subTitle
            title_tv_subtitle.visible()
        }
    }

    override fun subTitle(@StringRes resId: Int) = apply {
        title_tv_subtitle.isSelected = true
        title_tv_subtitle.setText(resId)
        title_tv_subtitle.visible()
    }

    override fun setIconPadding(@IconMold mold: Int, @DimenRes resId: Int) = apply {
        val padding = resDimenOffset(resId)
        when (mold) {
            ICON_NAVIGATION -> title_iv_navigation.setPadding(padding, padding, padding, padding)
            ICON_OPERATION -> title_iv_operation.setPadding(padding, padding, padding, padding)
            ICON_NEUTRAL -> title_iv_neutral.setPadding(padding, padding, padding, padding)
        }
    }

    override fun setIconPadding(@IconMold mold: Int, dp: Float) = apply {
        val padding = dp2px(dp)
        when (mold) {
            ICON_NAVIGATION -> title_iv_navigation.setPadding(padding, padding, padding, padding)
            ICON_OPERATION -> title_iv_operation.setPadding(padding, padding, padding, padding)
            ICON_NEUTRAL -> title_iv_neutral.setPadding(padding, padding, padding, padding)
        }
    }

    override fun navigationGoBack() = apply {
        navigationIcon(R.drawable.ic_arrow_back) {
            if (context is Activity) (context as Activity).onBackPressed()
        }
    }

    override fun navigationIcon(drawable: Drawable?, listener: (() -> Unit)?) = apply {
        if (drawable == null) {
            title_iv_navigation.gone()
            title_fl_navigation.gone()
        } else {
            title_iv_navigation.setImageDrawable(drawable)
            title_iv_navigation.visible()
            title_fl_navigation.visible()
        }
        title_tv_navigation.gone()
        if (listener == null) title_fl_navigation.setOnClickListener(null)
        else title_fl_navigation.doOnClick { listener.invoke() }
    }

    override fun navigationIcon(@DrawableRes resId: Int, listener: (() -> Unit)?) = apply {
        title_iv_navigation.setImageResource(resId)
        title_iv_navigation.visible()
        title_tv_navigation.gone()
        title_fl_navigation.visible()
        if (listener == null) title_fl_navigation.setOnClickListener(null)
        else title_fl_navigation.doOnClick { listener.invoke() }
    }

    override fun navigationText(text: CharSequence?, listener: (() -> Unit)?) = apply {
        if (text.isNullOrEmpty()) {
            title_tv_navigation.gone()
            title_fl_navigation.gone()
        } else {
            title_tv_navigation.text = text
            title_tv_navigation.visible()
            title_fl_navigation.visible()
        }
        title_iv_navigation.gone()
        if (listener == null) title_fl_navigation.setOnClickListener(null)
        else title_fl_navigation.doOnClick { listener.invoke() }
    }

    override fun navigationText(@StringRes resId: Int, listener: (() -> Unit)?) = apply {
        title_tv_navigation.setText(resId)
        title_tv_navigation.visible()
        title_iv_navigation.gone()
        title_fl_navigation.visible()
        if (listener == null) title_fl_navigation.setOnClickListener(null)
        else title_fl_navigation.doOnClick { listener.invoke() }
    }

    override fun operationIcon(drawable: Drawable?, listener: (() -> Unit)?) = apply {
        if (drawable == null) {
            title_iv_operation.gone()
        } else {
            title_iv_operation.setImageDrawable(drawable)
            title_iv_operation.visible()
        }
        title_tv_operation.gone()
        if (listener == null) title_iv_operation.setOnClickListener(null)
        else title_iv_operation.doOnClick { listener.invoke() }
    }

    override fun operationIcon(@DrawableRes resId: Int, listener: (() -> Unit)?) = apply {
        title_iv_operation.setImageResource(resId)
        title_iv_operation.visible()
        title_tv_operation.gone()
        if (listener == null) title_iv_operation.setOnClickListener(null)
        else title_iv_operation.doOnClick { listener.invoke() }
    }

    override fun operationText(text: CharSequence?, listener: (() -> Unit)?) = apply {
        if (text.isNullOrEmpty()) {
            title_tv_operation.gone()
        } else {
            title_tv_operation.text = text
            title_tv_operation.visible()
        }
        title_iv_operation.gone()
        if (listener == null) title_tv_operation.setOnClickListener(null)
        else title_tv_operation.doOnClick { listener.invoke() }
    }

    override fun operationText(resId: Int, listener: (() -> Unit)?) = apply {
        title_tv_operation.setText(resId)
        title_tv_operation.visible()
        title_iv_operation.gone()
        if (listener == null) title_tv_operation.setOnClickListener(null)
        else title_tv_operation.doOnClick { listener.invoke() }
    }

    override fun neutralIcon(drawable: Drawable?, listener: (() -> Unit)?) = apply {
        title_iv_neutral.setImageDrawable(drawable)
        title_iv_neutral.visible()
        title_tv_neutral.gone()
        if (listener == null) title_iv_neutral.setOnClickListener(null)
        else title_iv_neutral.doOnClick { listener.invoke() }
    }

    override fun neutralIcon(resId: Int, listener: (() -> Unit)?) = apply {
        title_iv_neutral.setImageResource(resId)
        title_iv_neutral.visible()
        title_tv_neutral.gone()
        if (listener == null) title_iv_neutral.setOnClickListener(null)
        else title_iv_neutral.doOnClick { listener.invoke() }
    }

    override fun neutralText(text: CharSequence?, listener: (() -> Unit)?) = apply {
        title_tv_neutral.text = text
        title_tv_neutral.visible()
        title_iv_neutral.gone()
        if (listener == null) title_tv_neutral.setOnClickListener(null)
        else title_tv_neutral.doOnClick { listener.invoke() }
    }

    override fun neutralText(resId: Int, listener: (() -> Unit)?) = apply {
        title_tv_neutral.setText(resId)
        title_tv_neutral.visible()
        title_iv_neutral.gone()
        if (listener == null) title_tv_neutral.setOnClickListener(null)
        else title_tv_neutral.doOnClick { listener.invoke() }
    }

    override fun getTitleTextView(): AppCompatTextView = title_tv_title
    override fun getSubtitleTextView(): AppCompatTextView = title_tv_subtitle
    override fun getNavigationIconView(): AppCompatImageView = title_iv_navigation
    override fun getNavigationTextView(): AppCompatTextView = title_tv_navigation
    override fun getOperationIconView(): AppCompatImageView = title_iv_operation
    override fun getOperationTextView(): AppCompatTextView = title_tv_operation
    override fun getNeutralIconView(): AppCompatImageView = title_iv_neutral
    override fun getNeutralTextView(): AppCompatTextView = title_tv_neutral

}